package org.myh.excel.example.multithreading;



import org.myh.excel.FieldSort;
import org.myh.excel.concurrent.MICClassDivisibleExcelBuffer;
import org.myh.excel.example.ExcelDto;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: MicReadDemo
 * Description:
 * date: 2024/3/26 14:04
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class MicReadDemo {

    public void demo() throws Exception {
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop", "宿舍信息上传模板.xlsx");
        InputStream inputStream = Files.newInputStream(path);
        MICClassDivisibleExcelBuffer micClassDivisibleExcelBuffer = new MICClassDivisibleExcelBuffer(inputStream, 2048, 500, 0, ExcelDto.class);

        //(不使用也可以直接使用），直接读取完从迭代器中获取数据即可
//       单线程读： micClassDivisibleExcelBuffer.readExcel();

        //使用多线程，若使用多线程则需要使用者自己开启线程或使用线程池，在读取的过程中抢占数据
        new Thread(()->{
            try {
                micClassDivisibleExcelBuffer.readExcel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(()->{
            while (micClassDivisibleExcelBuffer.hasNext()) {
                HashMap<Integer, List<FieldSort>> next =
                        micClassDivisibleExcelBuffer.next();
                if(next!=null){
                    System.out.println("线程1拿到数据:"+next.keySet().toString());
                }else {
                    continue;
                }
            }
        }).start();

        new Thread(()->{
            while (micClassDivisibleExcelBuffer.hasNext()) {
                HashMap<Integer, List<FieldSort>> next =
                        micClassDivisibleExcelBuffer.next();
                if(next!=null){
                    System.out.println("线程2拿到数据:"+next.keySet().toString());
                }else {
                    continue;
                }
            }
        }).start();
    }

    public static void main(String[] args) throws Exception {
        MicReadDemo micReadDemo = new MicReadDemo();
        micReadDemo.demo();
    }
}
